package com.captain.leetcode2.important;

import com.captain.leetcode.链表.ListNode;

import java.util.List;

/**
 * Des:
 * 链表删除问题 : 前指针的作用很重要
 *
 * @author XL
 * @Date 2021/9/1 8:54
 */
public class 删除排序链表中的重复元素II82 {

    public static void main(String[] args) {
        new 删除排序链表中的重复元素II82().deleteDuplicates(ListNode.getInstance());
    }

    //0 1,2,2,3,3,4,5
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) return null;
        ListNode dummy = ListNode.getDummy(head);
        ListNode cur = dummy;
        while (cur.next != null && cur.next.next != null) {
            //大粒度
            if (cur.next.val == cur.next.next.val) {
                //小粒度
                int val = cur.next.val;
                while (cur.next != null && cur.next.val == val) {
                    cur.next = cur.next.next;
                }
            } else {
                cur = cur.next;
            }
        }
        return dummy.next;
    }
}
